package com.zr.planClass.service.impl;

import com.zr.planClass.mapper.LegalPersonAndLegalPersonMapper;
import com.zr.planClass.model.legalandperson.*;
import com.zr.planClass.service.LegalPersonAndLegalPlanService;
import com.zr.util.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 非常狂躁的猪 on 2019/12/24.
 */
@Service
public class LegalPersonAndLegalPlanServiceImpl implements LegalPersonAndLegalPlanService {

    @Autowired
    private LegalPersonAndLegalPersonMapper legalPersonAndLegalPersonMapper;

    @Override
    public ResultVo queryAllPersonAndPlan(PersonAndPlanSelectVo personAndPlanSelectVo) {
        int userId = 1;
       // System.out.println("ppppppppppppppp"+personAndPlanSelectVo.getLegalPersonCode());
        List<LegalPlan> LegalPlanList = legalPersonAndLegalPersonMapper.queryAllPlan(personAndPlanSelectVo,userId);
        if (!CollectionUtils.isEmpty(LegalPlanList)){
            for(LegalPlan legalPlan:LegalPlanList){

                if (!legalPlan.getEnabled()){
                      legalPlan.setEnabledName(ErrorMessageEnum.JINYONG.getName());
                   // legalPlan.setEnabledName("禁用");
                }
                if (legalPlan.getEnabled()){
                    legalPlan.setEnabledName(ErrorMessageEnum.QIYONG.getName());
                    //legalPlan.setEnabledName("启用");
                }

            }
        }
        int count = legalPersonAndLegalPersonMapper.queryCount(personAndPlanSelectVo,userId);
        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(LegalPlanList);
        allRecords.setTotalNumber(count);
        allRecords.setPageIndex(personAndPlanSelectVo.getPageIndex());
        allRecords.setPageSize(personAndPlanSelectVo.getPageSize());
        allRecords.resetTotalNumber(count);

        return ResultBuilderVo.success(allRecords);


    }

    @Override
    public ResultVo queryLegalPerson(User user) {
        List<LegalPerson> list = legalPersonAndLegalPersonMapper.queryLegalPerson(user);
        List<LegalPersonVoBaoJiaZhuanYong> legalPersonVoBaoJiaZhuanYongs = new ArrayList<>();

        for (LegalPerson person:list){ //这块之所以做这样的修改是因为询价的前端字段被修改了报价的没有他俩公用一个法人接口
           LegalPersonVoBaoJiaZhuanYong legalPersonVoBaoJiaZhuanYong = new LegalPersonVoBaoJiaZhuanYong();
            BeanUtils.copyProperties(person,legalPersonVoBaoJiaZhuanYong);
            legalPersonVoBaoJiaZhuanYong.setId(person.getLegalPersonId());
            legalPersonVoBaoJiaZhuanYongs.add(legalPersonVoBaoJiaZhuanYong);
        }

        if (list.size()<= 0) {
            return ResultBuilderVo.error(ErrorMessageClass.UIDISNO);
        }
        return ResultBuilderVo.success(legalPersonVoBaoJiaZhuanYongs);
    }

    @Override
    @Transactional
    public ResultVo addLegalPlant(@Valid LegalPlanAddVo legalPlanAddVo) {
        ResultVo yanZheng = yanZzheng(legalPlanAddVo);
        if (!yanZheng.getSuccess()){
            return yanZheng;
        }

        LegalPlan legalPlan = new LegalPlan();
        BeanUtils.copyProperties(legalPlanAddVo,legalPlan);
        Date date = new Date();
        legalPlan.setCreateName("张三");
        legalPlan.setCreateTime(date);
        legalPlan.setUpdateName("张三");
        legalPlan.setUpdateTime(date);
        LegalPerson legalPerson = legalPersonAndLegalPersonMapper.getPersonIdByCode(legalPlanAddVo.getLegalPersonCode());
        legalPlan.setLegalPersonId(legalPerson.getLegalPersonId());
        legalPersonAndLegalPersonMapper.addPlan(legalPlan);
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo updatestatus(LegalPlantUpdateStatusVo legalPlantUpdateStatusVo) {
        Integer countPlan = legalPersonAndLegalPersonMapper.queryLegalPlanById(legalPlantUpdateStatusVo.getId());
        if (countPlan<= 0){
              return ResultBuilderVo.error(ErrorMessageEnum.GONGCHANGNO.getName());
            //return ResultBuilderVo.error("该工程不存在");
        }
        legalPersonAndLegalPersonMapper.updatestatus(legalPlantUpdateStatusVo);
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo update(@Valid LegalPlanUpdateVo legalPlanUpdateVo) {
        Integer countPlant = legalPersonAndLegalPersonMapper.queryLegaPlantByCode(legalPlanUpdateVo.getLegalPlantCode());
        if (countPlant<=0){
              return ResultBuilderVo.error(ErrorMessageEnum.GONGCHANGNO.getName());
           // return ResultBuilderVo.error("该工厂不存在");
        }
        //需要判断是否为禁用状态
        LegalPlan legalPlan2 = legalPersonAndLegalPersonMapper.queryLegaPlantEnabledByCode(legalPlanUpdateVo.getLegalPlantCode());
        if (legalPlan2.getEnabled()){
            return ResultBuilderVo.error("共创状态为启用");
        }
        LegalPlan legalPlan = new LegalPlan();
        BeanUtils.copyProperties(legalPlanUpdateVo,legalPlan);
        Date date = new Date();
        legalPlan.setUpdateTime(date);
        legalPlan.setUpdateName("张三");
        legalPersonAndLegalPersonMapper.update(legalPlan);
        return ResultBuilderVo.success();
    }

    public ResultVo yanZzheng(LegalPlanAddVo legalPlanAddVo){
        int countPerson = legalPersonAndLegalPersonMapper.queryLegalPersonByCode(legalPlanAddVo.getLegalPersonCode());
        if (countPerson<0){
              return ResultBuilderVo.error(ErrorMessageEnum.FARENNO.getName());
            //return ResultBuilderVo.error("请输入正确的法人");
        }
        int countPlantCode = legalPersonAndLegalPersonMapper.queryLegaPlantByCode(legalPlanAddVo.getLegalPlantCode());
        if (countPlantCode >=1){
              return ResultBuilderVo.error(ErrorMessageEnum.GONGCHANGBIANMA.getName());
           // return ResultBuilderVo.error("工厂编码不允许重复");
        }
        return  ResultBuilderVo.success();
    };
}































